SPORE.$Import('core.fx.css');

/*
---

name: Fx.Morph

description: Formerly Fx.Styles, effect to transition any number of CSS properties for an element using an object of rules, or CSS based selector rules.

license: MIT-style license.

requires: Fx.CSS

provides: Fx.Morph

...
*/

SPORE.register('core.fx.morph',function($ns){

	Fx.Morph = new Class({
	
		Extends: Fx.CSS,
	
		initialize: function(element, options){
			this.element = this.subject = document.id(element);
			this.parent(options);
		},
	
		set: function(now){
			if (typeof now == 'string') now = this.search(now);
			for (var p in now) this.render(this.element, p, now[p], this.options.unit);
			return this;
		},
	
		compute: function(from, to, delta){
			var now = {};
			for (var p in from) now[p] = this.parent(from[p], to[p], delta);
			return now;
		},
	
		start: function(properties){
			if (!this.check(properties)) return this;
			if (typeof properties == 'string') properties = this.search(properties);
			var from = {}, to = {};
			for (var p in properties){
				var parsed = this.prepare(this.element, p, properties[p]);
				from[p] = parsed.from;
				to[p] = parsed.to;
			}
			return this.parent(from, to);
		}
	
	});
	
	Element.Properties.morph = {
	
		set: function(options){
			this.get('morph').cancel().setOptions(options);
			return this;
		},
	
		get: function(){
			var morph = this.retrieve('morph');
			if (!morph){
				morph = new Fx.Morph(this, {link: 'cancel'});
				this.store('morph', morph);
			}
			return morph;
		}
	
	};
	
	Element.implement({
	
		morph: function(props){
			this.get('morph').start(props);
			return this;
		}
	
	});

});